'''
Created on 2010/09/11

@author: yukiyonezawa
'''
from pyrfid.DataProtocol.Driver.UHFDriver import UHFDriver
from pyrfid.DataProtocol.Driver.SessionManager import SessionManager
from pyrfid.DataProtocol.Driver.DriverInterface import DriverInterface
import time

class VirtualUHFDriver(UHFDriver):
    
    __LIFE_TIME = 0.3 #seconds
    __DRIVER_LEVEL = 0
    __IS_BLOCK = False
    __IS_INIT_VIRTUAL_TAG = True
    __INIT_MEMOERY = str()
    
    def __init__(self, is_block = False):
        self.__IS_BLOCK = is_block
        UHFDriver.__init__(self, self.__IS_BLOCK, self.__DRIVER_LEVEL, self.__LIFE_TIME)
        #initialize tag        
        
    def getLifeTime(self):
        return self.__LIFE_TIME
    
    def _turnOn(self):
        pass
    
    def _turnOff(self):
        pass
    
    def _writeCommand(self, tag_id, memory_type, start_address, write_data):
        print "Virtual UHF Driver write command execute =============="
        if memory_type == UHFDriver.UII:
            self.getSessionManager().getVirtualTag(tag_id, DriverInterface.UHF_VIRTUAL_TAG_TYPE).updateUII(start_address, write_data)
        elif memory_type == UHFDriver.USER:
            self.getSessionManager().getVirtualTag(tag_id, DriverInterface.UHF_VIRTUAL_TAG_TYPE).updateUserMemory(start_address, write_data)
        
        print '==============================================================='
        print tag_id, memory_type, start_address, write_data
        print '==============================================================='
        
    def _readCommand(self, tag_id, memory_type, start_address, width):
        
        return_value = ""
        if self.__IS_INIT_VIRTUAL_TAG == True:
            self.getSessionManager().getVirtualTag(tag_id, DriverInterface.UHF_VIRTUAL_TAG_TYPE).updateUserMemory(0, self.__INIT_MEMOERY)
            self.__IS_INIT_VIRTUAL_TAG = False
        
        if memory_type == UHFDriver.USER:
            #user memory
            return_value = self.getSessionManager().getVirtualTag(tag_id, DriverInterface.UHF_VIRTUAL_TAG_TYPE).getUserMemory(start_address, width)
        elif memory_type == UHFDriver.UII:
            #uii memory
            return_value = self.getSessionManager().getVirtualTag(tag_id, DriverInterface.UHF_VIRTUAL_TAG_TYPE).getUII(start_address, width)
                
        return return_value
    
    def initializeVirtualTag(self, initialize_memory):
        self.__INIT_MEMOERY = initialize_memory
        
    def _inventoryCommand(self):
        print "Virtual UHF Driver Inventory Command"
        return "1234", time.clock(), "12", "34" 
        
    def _selectCommand(self):
        print "Virtual UHF Driver Select Command"
        pass
    